package com.self.datebasecompare.config;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jhj
 * @description mysql和oracle数据映射表
 */
public class DatabaseTypeComparator {

    private static final Map<String, List<String>> TYPE_MAP = Arrays.stream(new String[][] {
            {"TINYINT", "NUMBER(3)"},
            {"BOOLEAN", "NUMBER(1)"},
            {"SMALLINT", "NUMBER(5)"},
            {"MEDIUMINT", "NUMBER(8)"},
            {"INT", "NUMBER(10)"},
            {"INTEGER", "NUMBER(10)"},
            {"BIGINT", "NUMBER(19)", "NVARCHAR2"},
            {"FLOAT", "FLOAT"},
            {"DOUBLE", "BINARY_DOUBLE"},
            {"DECIMAL", "NUMBER(38, 10)"},
            {"NUMERIC", "NUMBER(38, 10)"},
            {"DATE", "DATE"},
            {"TIME", "TIMESTAMP"},
            {"DATETIME", "DATE"},
            {"TIMESTAMP", "TIMESTAMP"},
            {"YEAR", "DATE"},
            {"CHAR", "CHAR(1)"},
            {"VARCHAR", "VARCHAR2", "NVARCHAR2", "CLOB", "NCLOB"},
            {"VARCHAR2", "VARCHAR2", "NVARCHAR2", "CLOB", "NCLOB"},
            {"TEXT", "VARCHAR2", "NVARCHAR2", "CLOB", "NCLOB"},
            {"BLOB", "BLOB"},
            {"BIT", "NUMBER(1)", "VARCHAR2"},
            {"GEOMETRY", "SDO_GEOMETRY"},
            {"JSON", "CLOB"}
    }).collect(Collectors.toMap(
            data -> data[0],
            data -> Arrays.stream(data).skip(1).collect(Collectors.toList())
    ));

    /**
     * 比较MySQL和Oracle的数据类型是否相等。
     * @param mysqlType MySQL的数据类型字符串
     * @param oracleType Oracle的数据类型整数
     * @return 如果类型相等返回true，否则返回false
     */
    public static boolean isTypeMatch(String mysqlType, String oracleType) {
        List<String> possibleOracleTypes = TYPE_MAP.getOrDefault(mysqlType.toUpperCase(), new ArrayList<>());
        String cleanOracleType = oracleType.toUpperCase().replaceAll("\\(.*\\)", "");

        // 去掉括号内容
        Boolean isMatch = possibleOracleTypes.stream()
                .map(type -> type.replaceAll("\\(.*\\)", "")) // Clean each Oracle type in the list
                .anyMatch(type -> type.equals(cleanOracleType));
        return isMatch;
    }

    public static List<String> getOracleTypes(String mysqlType) {
        return TYPE_MAP.getOrDefault(mysqlType.toUpperCase(), new ArrayList<>());
    }

    public static void main(String[] args) {
        // Test the method
        boolean isMatch = isTypeMatch("INT", "NUMBER");
        System.out.println("Is 'INT' a match for 'NUMBER'? " + isMatch);
    }
}
